/**
 * Created by George on 2015-06-29.
 */

'use strict';

console.log('Load O2O: ' + module.id);
let F = require('./function');
let C = require('../config');
let Dao = require('./dao');
let wareDispatcher = new Dao.ware();
let wareVersionDispatcher = new Dao.wareVersion();

function* api() {

}
exports.api = api;

/**
 * @todo API 添加商品
 * @param Env
 */
api.getNextWareId = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareDispatcher.getNextWareId(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

api.synchrodata = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareDispatcher.synchrodata(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
}

/**
 * @todo API 添加商品
 * @param Env
 */
api.add = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo API 更新商品
 * @param Env
 */
api.update = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I

  let ret = yield wareDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, '修改成功', 3, ret.data);
};

/**
 * @todo API 查询商品列表
 * @param Env
 * act==3,时为按关键字模糊查询商品列表
 */
api.list = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2, 3, 4]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret;
  if (fields.act == 3) {
    ret = yield  wareDispatcher.listByName(fields);
  } else {
    ret = yield  wareDispatcher.list(fields);
  }
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 商品详情
 * @param Env
 */
api.detail = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareDispatcher.detail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 添加商品版本
 * @param Env
 */
api.addVersion = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareVersionDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 更新商品版本
 * @param Env
 */
api.updateVersion = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareVersionDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo list商品版本
 * @param Env
 */
api.listVersion = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2, 3, 4, 5]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareVersionDispatcher.list(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 商品版本详情
 * @param Env
 */
api.detailVersion = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  let ret = yield wareVersionDispatcher.detail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 *@todo 联表查询
 */
if (C.env != 'production') {
  api.test = function*(Env) {
    Env.I = yield F.Init(Env, [1]);
    if (Env.I.errors) {
      return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
    }
    var ret = yield db.ware.findOne({name: 'nihao'}).populate('shops.sidInfo');//_id 2423dff34rd2324 id 112
    return F.returnMsg(200, 'OK', 3, ret);
  }
}




